package cloud.common.redis.redis.service.impl;

import cloud.common.redis.redis.service.IRedisService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Set;

/**
 * @author czq
 */
@Service
public class RedisServiceImpl implements IRedisService {

    /**
     * stringRedisTemplate只能缓存key-value的String类型
     */
    @Resource
    //@Qualifier("jedisConnectionFactory")
    protected RedisTemplate<String,Object> redisTemplate;

    @Override
    public Set<String> getKeys() {
        return this.redisTemplate.keys("*");
    }

    @Override
    public boolean deleteKey(String key) {
        if (this.redisTemplate.hasKey(key)){
            this.redisTemplate.delete(key);
            return true;
        }
        return false;
    }

    @Override
    public boolean hasKey(String key) {
        return redisTemplate.hasKey(key);
    }

    @Override
    public void setValue(String key, Object value) {
        ValueOperations<String,Object> vo = this.redisTemplate.opsForValue();
        vo.set(key,value);
    }

    @Override
    public Object getValue(String key,boolean isDelete) {
        ValueOperations<String,Object> vo = this.redisTemplate.opsForValue();
        Object object = vo.get(key);
        //删除相关的key键值
        if (isDelete){
            this.deleteKey(key);
        }
        return object;
    }


}
